<?php

/**
 * This is the model class for table "{{events}}".
 *
 * The followings are the available columns in table '{{events}}':
 * @property integer $id
 * @property integer $group_id
 * @property string $crdate
 * @property string $name
 * @property string $description
 * @property string $place
 * @property string $begin
 */
class Events extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Events the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{events}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name', 'required'),
			array('group_id,country_id', 'numerical', 'integerOnly'=>true),
			array('name, avatar, place, status', 'length', 'max'=>255),
			array('group_id, description, place, begin', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, group_id, crdate, name, description, place, begin', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'group'=>array(self::BELONGS_TO, 'Groups', 'group_id'),
			// Подписчики
			'countSubscribers' 	=> 	array(self::STAT, 'EventsUsers', 'event_id'),
			'subscribers' 		=> 	array(self::HAS_MANY, 'EventsUsers', 'event_id'),
			'subscribersUsers'	=>	array(self::HAS_MANY,'Users',array('user_id'=>'id'),
				'through'	=>	'subscribers',
				'select'	=>	'id, full_name, avatar',
				'limit' 	=> 	6,
				'order' 	=>	'RAND()',
			),
			'UsersAll'			=>	array(self::HAS_MANY,'Users',array('user_id'=>'id'),
				'through'	=>	'subscribers',
				'select'	=>	'id, full_name, avatar',
				'order' 	=>	'full_name',
			),
			'UsersAllUpdate'	=>	array(self::HAS_MANY,'Users',array('user_id'=>'id'),
				'through'	=>	'subscribers',
				'select'	=>	'id, full_name, avatar',
				'order' 	=>	'subscribers.id',
			),
			'checkUser' 		=> 	array(self::HAS_ONE, 'EventsUsers', 'event_id',
				'condition'	=>	'user_id=:user',
				'params'	=>	array(':user'=>Yii::app()->user->id)
			),
			// Ссылки
			'countUrls'			=>	array(self::STAT, 'EventsUrls', 'event_id'),
			'urls'				=>	array(self::HAS_MANY, 'EventsUrls', 'event_id',
				'limit' 	=> 	5,
				'order'		=>	'id DESC',
			),
			'urlsAll'				=>	array(self::HAS_MANY, 'EventsUrls', 'event_id',
				'order'		=>	'id DESC',
			),
			// Альбомы
			'avatar_info'=>array(self::BELONGS_TO, 'Photos', 'avatar'),	
			'countAlbums'		=>	array(self::STAT, 'Albums', 'event_id'),
			'albums'			=>	array(self::HAS_MANY, 'Albums', 'event_id'),
			// Музика
			'countMusics'=>array(self::STAT, 'MusicEvent', 'event_id'),
			'musics'=>array(self::HAS_MANY, 'MusicEvent', 'event_id',
				'limit' => 4,
				'order' => 'musics.id DESC',
			),
			// Videos
			'videos'=>array(self::HAS_MANY, 'VideoUser', 'event_id',
				'limit' => 10,
				'order' => 'RAND()',
			),
			'countVideos'=>array(self::STAT, 'VideoUser', 'event_id'),
			// Страна
			'country'			=>	array(self::BELONGS_TO, 'Country', 'country_id'),
			// Живая лента
			'countComments'		=>	array(self::STAT, 'EventsComments', 'event_id'),
			'comments'			=>	array(self::HAS_MANY, 'EventsComments', 'event_id',
				'limit' 	=> 	10,
				'order' 	=> 	'comments.id DESC',
				'condition'	=>	'comments.parent_id=0'
			),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'group_id' => 'Приглашает',
			'crdate' => 'Создан',
			'name' => 'Название',
			'avatar' => 'Аватар',
			'description' => 'Описание мероприятия',
			'country_id' => 'Страна',
			'place' => 'Место',
			'begin' => 'Начало',
		);
	}
	
	protected function beforeSave()
	{
		if(parent::beforeSave())
		{
			if($this->isNewRecord)
			{
				if(isset($_GET['group'])) 
					$this->group_id	= $_GET['group'];
				
				$this->crdate	=date('Y-m-d H:i:s');
			}
			return true;
		}
		else
			return false;
	}

	protected function beforeDelete()
	{
		if(parent::beforeDelete())
		{
			Albums::model()->deleteAll(
				'event_id=:event_id',
				array(':event_id'=>$this->id)
			);
			EventsComments::model()->deleteAll(
				'event_id=:event_id',
				array(':event_id'=>$this->id)
			);
			EventsUrls::model()->deleteAll(
				'event_id=:event_id',
				array(':event_id'=>$this->id)
			);
			EventsUsers::model()->deleteAll(
				'event_id=:event_id',
				array(':event_id'=>$this->id)
			);
			/*MusicEvent::model()->deleteAll(
				'event_id=:event_id',
				array(':event_id'=>$this->id)
			);*/
			VideoUser::model()->deleteAll(
				'event_id=:event_id',
				array(':event_id'=>$this->id)
			);
			return true;
		}
		else
			return false;
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('group_id',$this->group_id);
		$criteria->compare('crdate',$this->crdate,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('country_id',$this->country_id,true);
		$criteria->compare('place',$this->place,true);
		$criteria->compare('begin',$this->begin,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}